package org.sanm.utils;

import java.sql.Timestamp;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;

/**
 * @author zhengm
 * @description for java8 time
 */
public class DateUtil {
    public static String iosDateTimeOfPattern(String iosDateTime) {
        DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
        ZonedDateTime zonedDateTime = ZonedDateTime.parse(iosDateTime, formatter);
        DateTimeFormatter outputFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return zonedDateTime.format(outputFormatter);
    }


    public static LocalDateTime strToLocalDatetime(String datetimeStr) {
        return LocalDateTime.parse(datetimeStr, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static String localDatetimeToStr(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    public static LocalDate strToLocalDate(String dateStr) {
        return LocalDate.parse(dateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String localDateToStr(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    }

    public static String localDateToStrYm(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern("yyyy-MM"));
    }

    public static String timeToTimeStr(LocalTime localTime) {
        return localTime.format(DateTimeFormatter.ofPattern("HH:mm:ss"));
    }

    public static String localDateToStrNoLine(LocalDate localDate) {
        return localDate.format(DateTimeFormatter.ofPattern("yyyyMMdd"));
    }

    public static String timeToStrNoColon(LocalTime localTime) {
        return localTime.format(DateTimeFormatter.ofPattern("HHmmss"));
    }

    public static String localDateTimeToStrNoSeparate(LocalDateTime localDateTime) {
        return localDateTime.format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
    }


    public static String instantToLocalDateTime(Instant instant) {
        return localDatetimeToStr(LocalDateTime.ofInstant(instant,
                ZoneId.of(ZoneId.SHORT_IDS.get("CTT"))));

    }

    public static Instant localDateTimeToInstant(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8"));
    }


    public static long localDateTimeToMilli(LocalDateTime localDateTime) {
        return localDateTime.toInstant(ZoneOffset.of("+8")).toEpochMilli();
    }

    public static LocalDateTime millisToLocalDateTime(long millis) {
        Timestamp timestamp = new Timestamp(millis);
        return timestamp.toLocalDateTime();
    }

    public static long localDateTimeToSecond(LocalDateTime localDateTime) {
        return localDateTime.toEpochSecond(ZoneOffset.of("+8"));
    }

    /**
     * 日期间相差的天数
     */
    public static long diffOfDays(LocalDate startDate, LocalDate endDate) {
        return startDate.until(endDate, ChronoUnit.DAYS);
    }

    /**
     * 日期间相差的小时数
     */
    public static long diffOfHours(LocalDate startDate, LocalDate endDate) {
        return startDate.until(endDate, ChronoUnit.HOURS);
    }

    /**
     * 日期时间间相差的分钟
     */
    public static long diffOfMinutes(LocalDateTime startDateTime, LocalDateTime endDateTime) {
        return startDateTime.until(endDateTime, ChronoUnit.MINUTES);
    }

    /**
     * 日期时间间相差的秒
     */
    public static long diffOfSeconds(LocalDateTime startDateTime, LocalDateTime endDateTime) {
        return startDateTime.until(endDateTime, ChronoUnit.SECONDS);
    }

    /**
     * 是否当天
     */
    public static boolean isToday(LocalDateTime localDateTime) {
        return localDateTime
                .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"))
                .equals(localDateToStr(LocalDate.now()));
    }

    /**
     * 获取一天的开始时间, 即0点
     */
    public static LocalDateTime getDayStart(LocalDateTime localDateTime) {
        return localDateTime.withHour(0)
                .withMinute(0)
                .withSecond(0);
    }

    /**
     * 获取一天的结束时间
     */
    public static LocalDateTime getDayEnd(LocalDateTime localDateTime) {
        return localDateTime.withHour(23)
                .withMinute(59)
                .withSecond(59);
    }

    /**
     * 从现在到 23:59:59 相差到秒数
     */
    public static long diffSecFromNowToEnd() {
        return (LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).toEpochSecond(ZoneOffset.of("+8")))
                - (LocalDateTime.now().toEpochSecond(ZoneOffset.of("+8")));
    }

    public static String getYearMonthBefore(String yearMonth, int num) {
        return YearMonth.parse(yearMonth).minusMonths(num).toString();
    }

    public static String getYearBefore(String year, int num) {
        return Year.parse(year).minusYears(num).toString();
    }

    public static String getLocalDateStrBeforeYear(String localDateStr, int num) {
        return localDateToStr(strToLocalDate(localDateStr).minusYears(num));
    }

    public static String getLocalDateStrBeforeMonth(String localDateStr, int num) {
        return localDateToStr(strToLocalDate(localDateStr).minusMonths(num));
    }
}
